**********************************************
*A Republic If You Can Afford It - Ch 4 Do File
***Martha Kropf, Zach Mohr, Mary Jo McGowan, JoEllen Pope
***Stata version 18.0


***Set working directory
cd  //set path if different

***Confidence starting with SPAE2008-2012-2014-2016-2020 but only assessing through 2016. 

use "SPAE 2008-2012-2014-2016-2020.dta" 

***That is, 9 states meet the criteria for analysis:
**Alaska, California, Iowa, Maryland, Nebraska, New York, North Dakota, South Dakota, and Washington 


*4 additional states for robust check
*Delaware (2008; 2012), Indiana (2012; 2016), Florida (2012; 2016), and North Carolina (2008; 2012)

***rename inputstate regstate


keep if year==2008|year==2012|year==2016

***Year and CountyFIPS slightly different in expenditure dataset...


rename year Year
rename countyfips CountyFIPS


keep if regstate==02 | regstate==06 | regstate==19 | regstate==24 | regstate==31 |regstate==36|regstate==38 |regstate==46|regstate==53

***6000 obs.



***Note: if you do the 12-state analysis, use this statement: keep if regstate==02 | regstate==06 | regstate==19 | regstate==24 | regstate==31 |regstate==36|regstate==38 |regstate==53 |regstate==46|regstate==10 & Year==2008|regstate==10 & Year==2012|regstate==18 & Year==2012|regstate==18 & Year==2016|regstate==37 & Year ==2008|regstate==37 & Year==2012|regstate==12 & Year==2012|regstate==12 & Year==2016




keep weight q25 q29b q29c q8 q9 q10 q14 q15 q4 faminc income income_allyrs age gender race female educ degree pid7 Year CountyFIPS regstate birthyr 

***Robust check will include Delaware--only reports as state.

**replace CountyFIPS=10000 if CountyFIPS==10001
**replace CountyFIPS=10000 if CountyFIPS==10003
**replace CountyFIPS=10000 if CountyFIPS==10005


***Alaska only reports costs for whole state, so whole state is used for all counties
replace CountyFIPS=2000 if CountyFIPS==2020
replace CountyFIPS=2000 if CountyFIPS==2068
replace CountyFIPS=2000 if CountyFIPS==2090
replace CountyFIPS=2000 if CountyFIPS==2100
replace CountyFIPS=2000 if CountyFIPS==2110
replace CountyFIPS=2000 if CountyFIPS==2122
replace CountyFIPS=2000 if CountyFIPS==2130
replace CountyFIPS=2000 if CountyFIPS==2150
replace CountyFIPS=2000 if CountyFIPS==2170
replace CountyFIPS=2000 if CountyFIPS==2185
replace CountyFIPS=2000 if CountyFIPS==2188
replace CountyFIPS=2000 if CountyFIPS==2201
replace CountyFIPS=2000 if CountyFIPS==2232
replace CountyFIPS=2000 if CountyFIPS==2240
replace CountyFIPS=2000 if CountyFIPS==2290

replace CountyFIPS=2000 if CountyFIPS==2016
replace CountyFIPS=2000 if CountyFIPS==2050
replace CountyFIPS=2000 if CountyFIPS==2070
replace CountyFIPS=2000 if CountyFIPS==2220
replace CountyFIPS=2000 if CountyFIPS==2261
replace CountyFIPS=2000 if CountyFIPS==2270
replace CountyFIPS=2000 if CountyFIPS==2280


*change all NYC fipcodes to Manhattan Fips codes so it matches the cost database

replace CountyFIPS=36061 if CountyFIPS==36047
replace CountyFIPS=36061 if CountyFIPS==36081
replace CountyFIPS=36061 if CountyFIPS==36085
replace CountyFIPS=36061 if CountyFIPS==36005

merge m:m Year CountyFIPS using "General Dataset.dta"


drop if _merge==2

drop _merge



****reverse code confidence
gen yrconf1=q25
recode yrconf1 (1=4) (2=3) (3=2) (4=1) (5=.) (99=.)

***reverse code fraud. It will opposite confidence but the variable itself makes more sense. (big=more fraud)
gen fraud2=q29b
recode fraud2 (1=4) (2=3) (3=2) (4=1) (5=.) (9=.)(98=.)

gen fraud3=q29c
recode fraud3 (1=4) (2=3) (3=2) (4=1) (5=.) (9=.)(98=.)

recode fraud2 (99=.)
recode fraud3 (99=.)


label variable fraud2 "How often people steal/tamper with ballots--rec"

label variable fraud3 "How often people pretend to be someone else--rec"


factor fraud2 fraud3
predict index_twofraud

generate regproblem= q9
replace regproblem=1 if q9==2 & Year==2008
replace regproblem=0 if q9==1 & Year==2008
replace regproblem=0 if q9==2 & Year==2012
replace regproblem=0 if q9==2 & Year==2016
replace regproblem=0 if q9==3 

generate votingequp= q14
replace votingequp=1 if q14==2 & Year==2008
replace votingequp=0 if q14==1 & Year==2008
replace votingequp=0 if q14==2 & Year==2012
replace votingequp=0 if q14==2 & Year==2016


gen winner=0

replace winner=1 if pid7 < 4 & Year==2008
replace winner=1 if pid7 < 4 & Year==2012
replace winner=1 if pid7 > 4 & Year==2016


replace pid7=. if pid7==8
replace race=. if race==9


generate bymail=.
replace bymail=1 if q4==3
replace bymail=0 if q4==1
replace bymail=0 if q4==2
replace bymail=0 if q4==4
***makes the don't knows "not by mail"


generate logregvote=ln(newregvoters)


gen income_2012 =income if Year==2012
recode income_2012 (1=1)(2=2)(3=2)(4=3)(5=3)(6=4)(7=5)(8=6)(9=7)(10=8)(11=9)(12=10)(13=11)(14=12)(15=.)

generate income_2016=faminc
recode income_2016 (13=12)(14=12)(15=12)(16=12)(31=12)(97=.)

replace q15=. if q15==5
replace q15=. if q15==9
replace q15=. if q15==99



merge m:m CountyFIPS using "Rural-Urban Codes-2013.dta"


drop if _merge==2
drop _merge

***frequencies for Figures 4.1-4.3

tab yrconf1 [aweight=weight]

tab fraud2 [aweight=weight]

tab fraud3 [aweight=weight]

label variable sumrealcostperreg "E-FY 2-Year Sum of the Real Cost per Registered Voter"

***hierarchical model used in book. All voters.

mixed yrconf1 bymail income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 

margins, at(sumrealcostperreg=(1(2)123.9733) )
marginsplot, ytitle(Predicted Confidence) scheme(s1mono) recast(line) plot1opts(lcolor(gs8)) ciopt(color(black%20)) recastci(rarea) title("")

//graph export C:\Kropf\Figure4_4.jpg, quality(100) 


***robust check. plain old ols.
reg yrconf1 bymail income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]



***model in person voters

mixed yrconf1 q15 income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 

margins, at(sumrealcostperreg=(1(2)123.9733) )
marginsplot

**Robust #1 Rurality
mixed yrconf1 q15 income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg RUCC_2013 [pweight=weight]||regstate: ||CountyFIPS: 

***Robust #2 No weights
mixed yrconf1 q15 income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote||regstate: ||CountyFIPS: 


***#3 Robust including all voters (so no Q15) with and without bymail still significant


mixed yrconf1 income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 

mixed yrconf1 bymail income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 


***Washington state only with confidence. Note: expenses and expense squared not signif.

mixed yrconf1 bymail income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight] ||CountyFIPS: if regstate==53

mixed yrconf1 bymail income_allyrs age i.female i.race i.degree i.winner pid7 i.Year sumrealcostperreg logregvote [pweight=weight] ||CountyFIPS: if regstate==53


*****fraud used in book
mixed index_twofraud income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 

margins, at(sumrealcostperreg=(1(2)123.9733) )
marginsplot, ytitle(Predicted Belief in Fraud) scheme(s1mono) recast(line) plot1opts(lcolor(gs8)) ciopt(color(black%20)) recastci(rarea) title("")

 //graph export Figure4_5.jpg, quality(100)

estat icc

reg index_twofraud income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]

****Washington. Expenses not significant in just washington.
mixed index_twofraud income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]||CountyFIPS: if regstate==53


*********************************
***Robust #1 Rurality--same
mixed index_twofraud income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg RUCC_2013 [pweight=weight]||regstate: ||CountyFIPS: 

***No weights--same.

mixed index_twofraud income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote ||regstate: ||CountyFIPS: 

*****How well was polling place run?
mixed q8 income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 

mixed q8 income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 

***Problems with registration

mixed regproblem income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 
margins, at(sumrealcostperreg=(1(2)123.9733) )
marginsplot

mixed regproblem income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 

***pollworker ratings
mixed q15 income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 

mixed q15 income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 

***wait time

mixed q10 income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg##c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 

mixed q10 income_allyrs age i.female i.race i.degree i.winner pid7 i.Year c.sumrealcostperreg logregvote [pweight=weight]||regstate: ||CountyFIPS: 











